package com.guoxin.mashibing.concurrent;

import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;

public class CopyOnWriteListTest {

    public static void main(String[] args) {
        long begin = System.currentTimeMillis();
        List<String> list = new CopyOnWriteArrayList<>();
        Random r = new Random();
        Thread[] ths = new Thread[100];
        for (int i = 0; i < ths.length; i++) {
            ths[i] = new Thread(() -> {
                for (int j = 0; j < 500; j++) {
                    list.add("a");
                }
            });
        }
//
        Stream.of(ths).forEach(Thread::start);
//
        Stream.of(ths).forEach(t->{
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        long end = System.currentTimeMillis();
        System.out.println(end-begin);

    }
}
